package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.SaleListGoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SaleListGoodsService;
import com.atguigu.jxc.util.StringUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private SaleListGoodsDao saleListGoodsDao;

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for(int i = 4;i > intCode.toString().length();i--){

            unitCode = "0"+unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    // 分页查询商品库存信息
    @Override
    public PageInfo<Goods> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        PageHelper.startPage(page,rows);
        List<Goods> goodsList = goodsDao.listInventory(codeOrName,goodsTypeId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String sTime = sdf.format(new Date(0, 0, 1));
        String eTime = sdf.format(new Date());
        for (Goods goods : goodsList) {
            List<SaleList> saleListList = saleListGoodsDao.getSalelist(null, null, null, sTime, eTime);
            int saleTotal = 0;
            for (SaleList saleList : saleListList) {
                List<SaleListGoods> saleListGoods = saleListGoodsDao.getSaleListGoods(saleList.getSaleListId(), goods.getGoodsTypeId(), goods.getGoodsCode());
                saleTotal += saleListGoods.size();
            }
            goods.setSaleTotal(saleTotal);
        }
        PageInfo<Goods> pageResult = new PageInfo<>(goodsList);
        return pageResult;
    }

    // 获得单位列表
    @Override
    public List<Unit> unitList() {
        List<Unit> unitList = goodsDao.getUnitList();
        return unitList;
    }

    //查询所有商品信息（可以根据分类、名称查询）
    @Override
    public PageInfo<Goods> getGoodsList(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        PageHelper.startPage(page,rows);
        List<Goods> goodsList = goodsDao.getGoodsList(goodsName,goodsTypeId);
        PageInfo<Goods> pageResult = new PageInfo<>(goodsList);
        return pageResult;
    }

    // 添加或修改商品
    @Override
    public void save(Goods goods) {
        if(goods.getGoodsId()==null){
            // 添加
            goods.setState(0);
            goods.setInventoryQuantity(goods.getMinNum());
            goods.setLastPurchasingPrice(goods.getPurchasingPrice());
            // 获取上一个商品的编号
            String goodsCode = goodsDao.selectLastGoodsCode();
            if (!StringUtils.isEmpty(goodsCode)){
                goods.setGoodsCode(String.format("%04d",Integer.parseInt(goodsCode) + 1));
            }else {
                goods.setGoodsCode("0001");
            }
            goodsDao.addGoods(goods);
        }else {
            // 修改
            goodsDao.updateGoods(goods);
        }

    }

    // 删除商品
    @Override
    public void delete(Integer goodsId) {
        Goods goods = goodsDao.selectById(goodsId);
        if (goods.getState() == 0){
            goodsDao.deleteById(goodsId);
        }
    }

    // 分页查询无库存商品信息（可以根据商品名称或编码查询）
    @Override
    public PageInfo getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        PageHelper.startPage(page,rows);
        List<Goods> goodsList = goodsDao.getNoInventoryQuantity(nameOrCode);
        PageInfo<Goods> pageInfo = new PageInfo<>(goodsList);
        return pageInfo;
    }

    // 分页查询有库存商品信息（可以根据商品名称或编码查询）
    @Override
    public PageInfo getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        PageHelper.startPage(page,rows);
        List<Goods> goodsList = goodsDao.getHasInventoryQuantity(nameOrCode);
        PageInfo<Goods> pageInfo = new PageInfo<>(goodsList);
        return pageInfo;
    }

    // 添加商品期初库存
    @Override
    public void saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        goodsDao.saveStock(goodsId,inventoryQuantity,purchasingPrice);
    }

    // 删除库存（要判断商品状态 入库、有进货和销售单据的不能删除）
    @Override
    public void deleteStock(Integer goodsId) {
        Goods goods = goodsDao.selectById(goodsId);
        if(goods.getState() == 0){
            goodsDao.deleteStockById(goodsId);
        }
    }

    // 查询当前库存量 小于 库存下限的商品信息
    @Override
    public List<Goods> listAlarm() {
        List<Goods> goodsList = goodsDao.listAlarm();
        return goodsList;
    }
}
